﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Tww.Wpf.Controls.Controls.Shared.Enums;

namespace Tww.Wpf.Controls.Controls.Menus.ExpandMenu
{

    #region Imports

    #endregion

    public class ExpandMenuItem : TreeViewItem
    {
        #region  - Felder (öffentlich) -

        public static readonly DependencyProperty HoverBackgroundBrushProperty =
            DependencyProperty.Register("HoverBackgroundBrush",
                typeof (Brush), typeof (ExpandMenuItem), new PropertyMetadata(null));

        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof (Visual),
            typeof (ExpandMenuItem));

        public static readonly DependencyProperty IconTemplateProperty = DependencyProperty.Register("IconTemplate",
            typeof (DataTemplate), typeof (ExpandMenuItem), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedBackgroundBrushProperty =
            DependencyProperty.Register("SelectedBackgroundBrush",
                typeof (Brush), typeof (ExpandMenuItem), new PropertyMetadata(null));

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof (string),
            typeof (ExpandMenuItem), new PropertyMetadata("Title"));

        #endregion

        #region  - Konstruktoren -

        static ExpandMenuItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (ExpandMenuItem),
                new FrameworkPropertyMetadata(typeof (ExpandMenuItem)));
        }

        #endregion

        #region  - Indexer und Eigenschaften (öffentlich) -

        /// <summary>
        ///     Gets/sets the icon content template to show a custom icon.
        /// </summary>
        public DataTemplate IconTemplate
        {
            get { return (DataTemplate) GetValue(IconTemplateProperty); }
            set { SetValue(IconTemplateProperty, value); }
        }

        public Brush HoverBackgroundBrush
        {
            get { return (Brush) GetValue(HoverBackgroundBrushProperty); }
            set { SetValue(HoverBackgroundBrushProperty, value); }
        }

        public Brush SelectedBackgroundBrush
        {
            get { return (Brush) GetValue(SelectedBackgroundBrushProperty); }
            set { SetValue(SelectedBackgroundBrushProperty, value); }
        }

        /// <summary>
        ///     Gets or sets the title.
        /// </summary>
        /// <value>
        ///     The title.
        /// </value>
        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        /// <summary>
        /// Gets or sets the state of this Item
        /// </summary>
        /// <value>
        /// The state of the expand.
        /// </value>
        public ExpandState ExpandState
        {
            get { return IsExpanded ? ExpandState.Expanded : ExpandState.Collapsed; }
            set { IsExpanded = (value == ExpandState.Expanded); }
        }

        /// <summary>
        ///     Gets or sets the icon.
        /// </summary>
        /// <value>
        ///     The icon.
        /// </value>
        public Visual Icon
        {
            get { return (Visual) GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        #endregion

        #region  - Methoden (privat) -

        protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseLeftButtonUp(e);

            if (e.ClickCount == 1)
            {
                ChangeItemExpansion();
            }
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is ExpandMenuItem);
        }


        protected override DependencyObject GetContainerForItemOverride()
        {
            return new ExpandMenuItem();
        }

        private void ChangeItemExpansion()
        {
            switch (ExpandState)
            {
                case ExpandState.Expanded:
                    if (IsSelected)
                    {
                        IsExpanded = false;
                        IsSelected = false;
                    }
                    break;
                default:
                case ExpandState.Collapsed:
                    if (Items.Count > 0)
                    {
                        IsExpanded = true;
                    }
                    break;
            }
        }

        #endregion
    }
}